/*
'  DotNetNuke -  http://www.dotnetnuke.com
'  Copyright (c) 2002-2011
'  by DotNetNuke Corporation
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace DotNetNuke.Modules.Store.Catalog
{
	/// <summary>
	/// Summary description for TemplateInfo.
	/// </summary>
    public sealed class TemplateInfo
	{
        private static Regex regLogged = new Regex(@"\[IfLogged\](.+)\[/IfLogged\]", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private static Regex regNotLogged = new Regex(@"\[IfNotLogged\](.+)\[/IfNotLogged\]", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private string name = string.Empty;
        private string path = string.Empty;
        private string content = string.Empty;
        private List<Token> tokens = null;

		public TemplateInfo(string file, bool parse, bool IsLogged)
		{
            if (File.Exists(file))
            {
                string fileContent;
                FileInfo fileInfo = new FileInfo(file);
                using (StreamReader reader = new StreamReader(fileInfo.FullName))
                {
                    fileContent = reader.ReadToEnd();
                    reader.Close();
                }
                name = fileInfo.Name;
                path = fileInfo.FullName;
                if (parse == true)
                {
                    content = ParseCondition(fileContent, IsLogged);
                    string[] tokenContent = content.Split(new char[] { '[', ']' });
                    tokens = new List<Token>(tokenContent.Length);
                    foreach (string token in tokenContent)
                    {
                        Token tokenTemplate = new Token(token);
                        tokens.Add(tokenTemplate);
                    }
                }
                else
                    content = fileContent;
            }
            else
                throw new FileNotFoundException();
		}

		public string Name
		{
			get {return name;}
		}

		public string Path
		{
			get {return path;}
		}

		public string Content
		{
			get {return content;}
		}

        public List<Token> Tokens
		{
            get { return tokens; }
		}

        private string ParseCondition(string fileContent, bool IsLogged)
        {
            string temp = regLogged.Replace(fileContent, IsLogged ? "$1" : "");
            return regNotLogged.Replace(temp, !IsLogged ? "$1" : "");
        }
	}
}
